package edu.scau.mis.web.controller.pos;

import edu.scau.mis.pos.domain.Payment;
import edu.scau.mis.pos.mapper.PaymentMapper;
import edu.scau.mis.pos.service.IPaymentService;
import edu.scau.mis.core.domain.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;

/**
 * (Payment)表控制层
 *
 * @author CJW
 * @since 2024-04-17 23:51:46
 */
@RestController
@RequestMapping("payment")
public class PaymentController {
    /**
     * 服务对象
     */
    @Autowired
    private IPaymentService paymentService;

    /**
     * 查询数据列表
     *
     * @param payment 筛选条件
     * @return 查询结果
     */
    @GetMapping("/list")
    @ApiOperation("查询数据列表")
    public AjaxResult list(Payment payment) {
        return AjaxResult.success(this.paymentService.selectList(payment));
    }
    
    /**
     * 分页查询数据列表
     *
     * @param payment 筛选条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询数据列表")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Payment payment) {
        PageHelper.startPage(pageNum,pageSize);
        List<Payment> list = this.paymentService.selectList(payment);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("通过主键查询单条数据")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return AjaxResult.success(this.paymentService.selectById(id));
    }

    /**
     * 新增数据
     *
     * @param payment 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增数据")
    public AjaxResult add(@RequestBody Payment payment) {
        int rows = paymentService.createPayment(payment);
        return (rows > 0 && rows != 0) ? AjaxResult.success("生成支付单成功") : AjaxResult.error("生成支付单失败");
    }

    /**
     * 编辑数据
     *
     * @param payment 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑数据")
    public AjaxResult edit(@RequestBody Payment payment) {
        return AjaxResult.success(this.paymentService.update(payment));
    }

    /**
     * 批量删除数据
     *
     * @param ids 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("批量删除数据")
    public AjaxResult deleteById(@PathVariable Long ids) {
        return AjaxResult.success(this.paymentService.deleteByIds(ids));
    }
    /**
     * 按月份统计销售额，并且可以按年份筛选数据
     *
     */
    @GetMapping("/line")
    @ApiOperation("按月份统计销售额，并且可以按年份筛选数据")
    public AjaxResult getSalesByMonthAndYear(@RequestParam("year") int year) {
        List<Map<String, Object>> resultList = this.paymentService.getSalesByMonthAndYear(year);
        return AjaxResult.success("获取销售额成功！", resultList);
    }
    /**
     * 按发型师统计业绩金额，可以按时间筛选数据
     *
     */
    @GetMapping("/search")
    @ApiOperation("按发型师统计业绩金额，可以按时间筛选数据")
    public AjaxResult searchStylistPerformance(@RequestParam String timeUnit, @RequestParam String searchTime) {
        List<Map<String, Object>> resultList = paymentService.getStylistPerformanceByTimeUnit(timeUnit, searchTime);
        return AjaxResult.success("获取发型师业绩成功", resultList);
    }
}
